# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import psutil
import os
import serial
import serial.tools.list_ports
import threading
import time
com_rx_buf = ''				# 接收缓冲区
com_tx_buf = ''				# 发送缓冲区
COMM = serial.Serial()		# 定义串口对象
port_list: list				# 可用串口列表
port_select: list			# 选择好的串口


# 无串口返回0，
# 返回可用的串口列表
def get_com_list():
    global port_list
    # a = serial.tools.list_ports.comports()
    # print(a)
    # port_list = list(serial.tools.list_ports.comports())
    port_list = serial.tools.list_ports.comports()
    return port_list


def set_com_port(n=0):
    global port_list
    global port_select
    port_select = port_list[n]
    return port_select.device


# 打开串口
def serial_open(n=0):
    global COMM
    serial_port = set_com_port(n)
    COMM = serial.Serial(serial_port, 115200, timeout=0.01)
    if COMM.isOpen():
        print(serial_port, "open success")
        return 0
    else:
        print("open failed")
        return 255


# 关闭串口
def serial_close():
    global COMM
    COMM.close()
    print(COMM.name + "closed.")


def set_com_rx_buf(buf=''):
    global com_rx_buf
    com_rx_buf = buf


def set_com_tx_buf(buf=''):
    global com_tx_buf
    com_tx_buf = buf


def get_com_rx_buf():
    global com_rx_buf
    return com_rx_buf


def get_com_tx_buf():
    global com_tx_buf
    return com_tx_buf


def thread_com_receive():
    while True:
        try:
            rx_buf = ''
            rx_buf = COMM.read()  # 转化为整型数字
            if rx_buf != b'':
                time.sleep(0.01)
                rx_buf = rx_buf + COMM.read_all()
                print("串口收到消息:", rx_buf)
            time.sleep(0.01)
        except:
            pass
    pass


# def serial_encode(addr=0, command=0, param1=0, param0=0):
#     buf = [addr, command, param1, param0, 0, 0, 0, 0]
#     print(buf)
#     return buf


def serial_send_command(addr=0, command=0, param1=0, param0=0, data3=0, data2=0, data1=0, data0=0):
    buf = [addr, command, param1, param0, data3, data2, data1, data0]
    COMM.write(buf)
    pass

def get_cpu_mem():
    pid = os.getpid()
    p = psutil.Process(pid)
    cpu_percent = p.cpu_percent()
    mem_percent = p.memory_percent()
    print("cpu:{:2d}%,mem:{:2d}%".format(int(psutil.cpu_percent()), int(psutil.virtual_memory().percent)))
    print("cpu:{:2f}%,mem:{:2f}%".format(cpu_percent,   mem_percent))

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    get_com_list()
    len = port_list.__len__()
    device = port_list[1].device
    print(len, device)
    serial_open(1)
    thread1 = threading.Thread(target=thread_com_receive)
    thread1.start()
    while 1:
        buf = ("tstfun lvgl "+
               "{:03d}{:03d}{:03d}".format(int(psutil.cpu_percent()),
                                     int(psutil.virtual_memory().percent),
                                     int(psutil.swap_memory().percent))+"\n").encode()
        print(buf)
        COMM.write(buf)
        time.sleep(1)



